<?php
/**
 * Created by 1971599474@qq.com
 * User: zhixuan
 * Date: 2020/1/22
 * Time: 18:13
 */

namespace app\common\controller;


use app\common\model\SpecGoodsPrice;
use app\common\model\Goods as GoodsModel;
use app\common\model\Discount as DiscountModel;
use app\common\utils\JsonService;
use think\response\Json;

class Goods extends BaseController
{
    public function getSpecGodsPrice(){
        if($this->request->isAjax()){
            $spec_key = $this->request->post('spec_key');
            $goods_id = $this->request->post('goods_id');
            sort($spec_key);
            $key = implode('_',$spec_key);
            $specPrice = SpecGoodsPrice::getOne(['key'=>$key,'goods_id'=>$goods_id]);
            $goods = GoodsModel::getOne(['goods_id'=>$goods_id],'goods_id,exchange_integral');

            //是否折扣商品
            $time = time();
            $where[] = ['goods_id','=',$goods_id];
            $where[] = ['start_time','<',$time];
            $where[] = ['end_time','>',$time];
            $discount = DiscountModel::getOne($where);
            if(!empty($discount) && $goods['exchange_integral'] > 0){
                $specPrice['price'] = ($specPrice['price']*$discount['rate']/100).'+'.$goods['exchange_integral'].'积分';
            }else if(!empty($discount)){
                $specPrice['price'] = $specPrice['price']*$discount['rate']/100;
            }else if($goods['exchange_integral'] > 0){
                $specPrice['price'] = $specPrice['price'].'+'.$goods['exchange_integral'].'积分';
            }


            return JsonService::success($specPrice);
        }
    }
}